﻿using Newtonsoft.Json;
using System;
using System.IO;
using System.Runtime.Serialization;

namespace eslib.tools.config
{
    /// <summary>
    /// json配置文件操作工具
    /// </summary>
    public static class GenericJsonConfig
    {
        /// <summary>
        /// 储存对象返回json
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string Save(object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        /// <summary>
        /// 储存对象返回json
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static string Save(object obj, JsonSerializerSettings settings)
        {
            return JsonConvert.SerializeObject(obj, settings);
        }


        /// <summary>
        /// 储存对象,保存到文件路径
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="filePath"></param>
        public static void Save(object obj, string filePath)
        {
            string json = Save(obj);
            File.WriteAllText(filePath, json);
        }

        /// <summary>
        /// 储存对象,保存到文件路径
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="filePath"></param>
        /// <param name="settings"></param>
        public static void Save(object obj, string filePath, JsonSerializerSettings settings)
        {
            string json = Save(obj, settings);
            File.WriteAllText(filePath, json);
        }


        /// <summary>
        /// 储存对象返回json（以小写开头）
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SaveWithLCase(object obj)
        {
            JsonSerializerSettings setting = new();
            setting.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
            return JsonConvert.SerializeObject(obj, setting);
        }


        /// <summary>
        /// 从文件加载 
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static dynamic LoadDynamic(string filePath)
        {
            return LoadDynamicFromString(File.ReadAllText(filePath));
        }




        /// <summary>
        /// 从json字符串加载,如果字符串为null或者""，返回空对象
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static dynamic LoadDynamicFromString(string json)
        {
            if (json == null || json == "")
                return new object();
            else
                return JsonConvert.DeserializeObject(json);
        }



        /// <summary>
        /// 从文件加载 
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T Load<T>(string filePath) where T : new()
        {
            return LoadFromString<T>(File.ReadAllText(filePath));
        }


        /// <summary>
        /// 从json字符串加载,如果字符串为null或者""，返回new构造对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T LoadFromString<T>(string json) where T : new()
        {
            if (json == null || json == "")
                return new T();
            else
                return JsonConvert.DeserializeObject<T>(json);
        }
    }
}
